'use strict'

const wkthread = require('worker_threads')

//线程启动内部服务。
function runWorkerThread (pathfile, events = {}) {
    
  let subserv = new wkthread.Worker(pathfile)

  for (let k in events) {
    if (typeof events[k] !== 'function') {
      continue
    }
    
    subserv.on(k, events[k])
  }

  subserv.on('error', err => {
    console.error(err)
  })

  subserv.on('exit', (code) => {
    runWorkerThread(pathfile, events)
  })
  
  return subserv
}

module.exports = runWorkerThread
